﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace VoicePhone.Controls
{
    /// <summary>
    /// Calendar.xaml 的交互逻辑
    /// </summary>
    public partial class Calendar : UserControl
    {
        public Calendar()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        public Action<DateTime> CallBackAction;
        public Dictionary<int, string> DicMonths = new Dictionary<int, string>()
        {
            { 1,"January"},{ 2,"February"},{ 3,"March"},{ 4,"April"},{ 5,"May"},{ 6,"June"},
            { 7,"July"},{ 8,"August"},{ 9,"September"},{ 10,"October"},{ 11,"November"},{ 12,"December"}
        };

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.comMonths.ItemsSource = this.DicMonths;
            this.RefMonthDate();
        }

        private void RefMonthDate()
        {
            DateTime firstDay = new DateTime(this.Year, this.Month, 1);
            DateTime endDay = firstDay.AddMonths(1).AddDays(-1);
            DateTime previousEndDay = firstDay.AddDays(-1);
            this.gridDay.Children.Clear();
            for (int i = 1; i < 43; i++)
            {
                BaseButton btn = new BaseButton()
                {
                    Margin = new Thickness(2),
                    HorizontalAlignment = HorizontalAlignment.Center,
                    HorizontalContentAlignment = HorizontalAlignment.Center,
                    Foreground = i % 7 == 1 || i % 7 == 0 ? Brushes.Red : Brushes.Black
                };
                if (i - 1 >= (int)firstDay.DayOfWeek)
                {
                    btn.Content = i - (int)firstDay.DayOfWeek;
                    btn.Tag = firstDay;
                    if (firstDay.Year == DateTime.Now.Year && firstDay.Month == DateTime.Now.Month &&
                        DateTime.Now.Day == i - (int)firstDay.DayOfWeek)
                        btn.Background = new SolidColorBrush(Color.FromRgb(110, 172, 242));
                }
                else
                {
                    btn.Content = previousEndDay.Day - (int)firstDay.DayOfWeek + i;
                    btn.Foreground = Brushes.DimGray;
                    btn.Tag = previousEndDay;
                }
                if (i > endDay.Day + (int)firstDay.DayOfWeek)
                {
                    btn.Content = i - endDay.Day - (int)firstDay.DayOfWeek;
                    btn.Foreground = Brushes.DimGray;
                    btn.Tag = firstDay.AddMonths(1);
                }
                this.gridDay.Children.Add(btn);
            }
        }

        private void BtnDate_Click(object sender, RoutedEventArgs e)
        {
            DateTime dateTime = Convert.ToDateTime((e.Source as Button).Tag);
            this.CallBackAction?.Invoke(new DateTime(dateTime.Year, dateTime.Month, int.Parse((e.Source as Button).Content.ToString())));
        }

        public DateTime CurDate { get; set; }

        public int Year
        {
            get { return (int)GetValue(YearProperty); }
            set { SetValue(YearProperty, value); }
        }

        public static readonly DependencyProperty YearProperty =
            DependencyProperty.Register("Year", typeof(int), typeof(Calendar), new PropertyMetadata(DateTime.Now.Year));


        public int Month
        {
            get { return (int)GetValue(MonthProperty); }
            set { SetValue(MonthProperty, value); }
        }

        public static readonly DependencyProperty MonthProperty =
            DependencyProperty.Register("Month", typeof(int), typeof(Calendar), new PropertyMetadata(DateTime.Now.Month, MonthChanged));


        public int Day
        {
            get { return (int)GetValue(DayProperty); }
            set { SetValue(DayProperty, value); }
        }

        public static readonly DependencyProperty DayProperty =
            DependencyProperty.Register("Day", typeof(int), typeof(Calendar), new PropertyMetadata(DateTime.Now.Day));


        private static void MonthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var calendar = d as Calendar;
            calendar.RefMonthDate();
        }

        private void BtnNext_Click(object sender, RoutedEventArgs e)
        {
            Year = Month == 12 ? Year + 1 : Year;
            Month = Month == 12 ? 1 : Month + 1;
        }

        private void BtnPrevious_Click(object sender, RoutedEventArgs e)
        {
            Year = Month == 1 ? Year - 1 : Year;
            Month = Month == 1 ? 12 : Month - 1;
        }
    }
}
